{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Max Pair Sum in an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maxSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #数组中的最大数对和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。请你从 <code>nums</code> 中找出和 <strong>最大</strong> 的一对数，且这两个数数位上最大的数字相等。</p>\n",
    "\n",
    "<p>返回最大和，如果不存在满足题意的数字对，返回 <code>-1</code><em> 。</em></p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [51,71,17,24,42]\n",
    "<strong>输出：</strong>88\n",
    "<strong>解释：</strong>\n",
    "i = 1 和 j = 2 ，nums[i] 和 nums[j] 数位上最大的数字相等，且这一对的总和 71 + 17 = 88 。 \n",
    "i = 3 和 j = 4 ，nums[i] 和 nums[j] 数位上最大的数字相等，且这一对的总和 24 + 42 = 66 。\n",
    "可以证明不存在其他数对满足数位上最大的数字相等，所以答案是 88 。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [1,2,3,4]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>不存在数对满足数位上最大的数字相等。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [max-pair-sum-in-an-array](https://leetcode.cn/problems/max-pair-sum-in-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [max-pair-sum-in-an-array](https://leetcode.cn/problems/max-pair-sum-in-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[51,71,17,24,42]', '[1,2,3,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:\n",
    "        addlist = []\n",
    "        currentMax = 0\n",
    "        maxSum = 0\n",
    "\n",
    "        for i in nums:\n",
    "\n",
    "            if i == 1:\n",
    "                currentMax = max(currentMax+i,i)\n",
    "                maxSum = max(currentMax,maxSum)\n",
    "                addlist.append(maxSum)\n",
    "            \n",
    "            else:\n",
    "                addlist.append(maxSum)\n",
    "                maxSum = 0\n",
    "                currentMax = 0\n",
    "        \n",
    "        return max(addlist)\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, A: List[int], res=-1) -> int:\n",
    "        hi = [0] * 10\n",
    "        for x in A:\n",
    "            y = max(map(int, str(x)))\n",
    "            if hi[y] and res < hi[y] + x:\n",
    "                res = hi[y] + x\n",
    "            if hi[y] < x:\n",
    "                hi[y] = x\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "\n",
    "        # oneValue = max(nums)\n",
    "        # nums.remove(oneValue)\n",
    "        # twoValue = max(nums)\n",
    "\n",
    "        # oneMax = max(str(oneValue))\n",
    "        # twoMax = max(str(twoValue))\n",
    "\n",
    "        # if oneMax == twoMax:return oneValue + twoValue\n",
    "        # else:return -1\n",
    "\n",
    "        # def Filter(n,value):\n",
    "        #     return int(max(str(n))) == value\n",
    "\n",
    "\n",
    "\n",
    "        ans = 0\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            maxValue = int(max(str(nums[i])))\n",
    "            equalList = list(filter(lambda x:int(max(str(x))) == maxValue,nums[0:i] + nums[i + 1:]))\n",
    "            if not equalList:\n",
    "                continue\n",
    "            else:\n",
    "                twoValue = max(equalList)\n",
    "                if twoValue + nums[i] > ans:\n",
    "                    ans = twoValue + nums[i]  \n",
    "\n",
    "        if ans:return ans\n",
    "        else:return -1\n",
    "\n",
    "            \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans=-1\n",
    "        max_val=[-inf]*10\n",
    "        for v in nums:\n",
    "            max_d=max(map(int,str(v)))\n",
    "            ans=max(ans,v+max_val[max_d])\n",
    "            max_val[max_d]=max(max_val[max_d],v)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def split_digits(self, num):\n",
    "        dlist = []\n",
    "        nlist = list(str(num))\n",
    "\n",
    "        for i in range(len(nlist)):\n",
    "            dlist.append(int(nlist[i]))\n",
    "        return dlist\n",
    "\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        result = -1\n",
    "\n",
    "        l = []\n",
    "        for i in range(len(nums)):\n",
    "            l.append(self.split_digits(nums[i]))\n",
    "\n",
    "        for i in range(len(nums) - 1):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                if max(l[i]) == max(l[j]):\n",
    "                    result = max(result, nums[i] + nums[j])\n",
    "\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        dict = {}\n",
    "        for num in nums :\n",
    "            tmp = num\n",
    "            max_v = 0\n",
    "            while tmp > 0 :\n",
    "                v = tmp % 10\n",
    "                max_v = max(max_v,v)\n",
    "                tmp = tmp // 10\n",
    "                pass\n",
    "            if dict.__contains__(max_v) :\n",
    "                dict[max_v].append(num)\n",
    "                pass\n",
    "            else:\n",
    "                dict[max_v] = [num]\n",
    "                pass\n",
    "            pass\n",
    "        result = -1\n",
    "\n",
    "        for k,v in dict.items() :\n",
    "            if len(v) < 2 :\n",
    "                continue\n",
    "            v.sort()\n",
    "            result = max(result,v[-1] + v[-2])\n",
    "            pass\n",
    "        return result\n",
    "        pass\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        \"\"\"\n",
    "        数据范围看起来可以暴力?\n",
    "        如何找出一个数数位最大的数?数字最多四位，不断整除？\n",
    "        如何用小于n^2的时间复杂度找出满足要求的两个数？ \n",
    "        我的思路：根据最大数位分组，在每个分组中找出最大的两个数求和，再在所有数位中找出最大的和\n",
    "        dic = {1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[]}\n",
    "        ans = -1\n",
    "        for num in nums:\n",
    "            ma = 0\n",
    "            tmp = num\n",
    "            while tmp:\n",
    "                ma = max(ma, tmp%10)\n",
    "                tmp //= 10\n",
    "            dic[ma].append(num)\n",
    "        for v in dic.values():\n",
    "            v.sort()\n",
    "            if len(v) >= 2:\n",
    "                ans = max(ans, v[-1] + v[-2])\n",
    "        return ans\n",
    "        \"\"\"\n",
    "        ans = -1\n",
    "        max_val = [-inf] * 10\n",
    "        for v in nums:\n",
    "            # 转换为字符串，再通过map转换为数字\n",
    "            max_d = max(map(int, str(v)))\n",
    "            ans = max(ans, v + max_val[max_d])\n",
    "            max_val[max_d] = max(max_val[max_d], v)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = -1\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                max1 = max(_ for _ in list(str(nums[i])))\n",
    "                max2 = max(_ for _ in list(str(nums[j])))\n",
    "                if max1 == max2:\n",
    "                    ans = max(ans, nums[i] + nums[j])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        cnt = [0] * 10\n",
    "        ans = -1\n",
    "        for x in nums:\n",
    "            if cnt[int(max(str(x)))]:\n",
    "                ans = max(ans, x + cnt[int(max(str(x)))])\n",
    "            cnt[int(max(str(x)))] = max(cnt[int(max(str(x)))], x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans=-1 #初始化成-1\n",
    "        max_val=[-inf]*10 #维护每个数位对应的最大值，初始化成负无穷大\n",
    "        for v in nums:\n",
    "            max_d=max(map(int,str(v))) #最大数位，将数字转成字符串，求最大的数字\n",
    "            ans=max(ans,v+max_val[max_d]) #更新最大值,v+前面最大的数位\n",
    "            max_val[max_d]=max(max_val[max_d],v)#更新位上的最大值\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ls = [[] for _ in range(10)]\n",
    "        ans = -1 \n",
    "        for i in nums:\n",
    "            ls[int(max(list(str(i))))-1].append(i)\n",
    "        for i in ls:\n",
    "            tmp = sorted(i)\n",
    "            if len(tmp) < 2:\n",
    "                continue\n",
    "            ans = max(ans,tmp[-1]+tmp[-2])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        \"\"\"\n",
    "        这道题的思路：\n",
    "        首先用一个数组来记录最大的单个数字，对应的整个数\n",
    "        每次碰到相同的最大的单个数字，就试下是不是最大的ans\n",
    "        \"\"\"\n",
    "        ans = -1\n",
    "        max_nums = [-inf]*10\n",
    "        for v in nums:\n",
    "            max_d = max(map(int, str(v)))\n",
    "            ans = max(ans, v+max_nums[max_d])\n",
    "            max_nums[max_d] = max(max_nums[max_d], v)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        def max_digit(num):\n",
    "            res=num%10\n",
    "            while num>=10:\n",
    "                num=num//10\n",
    "                res=max(res,num%10)\n",
    "            return res\n",
    "                \n",
    "        res=-1\n",
    "        n=len(nums)\n",
    "        for i in range(n-1):\n",
    "            for j in range(i+1,n):\n",
    "                if max_digit(nums[i])==max_digit(nums[j]):\n",
    "                    res=max(res,nums[i]+nums[j])\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def split_digits(self, num):\n",
    "        dlist = []\n",
    "        nlist = list(str(num))\n",
    "\n",
    "        for i in range(len(nlist)):\n",
    "            dlist.append(int(nlist[i]))\n",
    "        return dlist\n",
    "\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        result = -1\n",
    "\n",
    "        l = []\n",
    "        for i in range(len(nums)):\n",
    "            l.append(self.split_digits(nums[i]))\n",
    "\n",
    "        for i in range(len(nums) - 1):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                if max(l[i]) == max(l[j]):\n",
    "                    result = max(result, nums[i] + nums[j])\n",
    "\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        dct = defaultdict(list)\n",
    "        for n in nums:\n",
    "            dct[max(str(n))].append(n)\n",
    "        cand = []\n",
    "        for v in dct.values():\n",
    "            if len(v) >= 2:\n",
    "                v.sort()\n",
    "                cand.append(sum(v[-2:]))\n",
    "        return max(cand, default=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        memo = dict()\n",
    "        for num in nums:\n",
    "            maxnum = int(max(str(num)))\n",
    "            if maxnum not in memo:\n",
    "                memo[maxnum] = [num]\n",
    "            else:\n",
    "                memo[maxnum].append(num)\n",
    "        ans = -1\n",
    "        for x,y in memo.items():\n",
    "            if len(y) >= 2:\n",
    "                y.sort()\n",
    "                ans = max(ans,y[-1] + y[-2])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, a: List[int]) -> int:\n",
    "        h=defaultdict(list)\n",
    "        for i in a:\n",
    "            h[max(str(i))].append(i)\n",
    "        ans=-1\n",
    "        for t in h.values():\n",
    "            if len(t)>=2:\n",
    "                t.sort(reverse=True)\n",
    "                ans=max(ans,t[0]+t[1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        max_val = [-inf] * 10\n",
    "        for v in nums:\n",
    "            max_d = max(map(int, str(v)))\n",
    "            ans = max(ans, v + max_val[max_d])\n",
    "            max_val[max_d] = max(max_val[max_d], v)\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        b = []\n",
    "        w = -1\n",
    "        for i in range(0,n):\n",
    "            k =nums[i]\n",
    "            b.append(0)\n",
    "            while(k):\n",
    "                b[i]=max(b[i],k%10)\n",
    "                k=k//10\n",
    "        for i in range(n):         \n",
    "            for j in range(i+1,n):\n",
    "                if (b[i] == b[j]):\n",
    "                    w=max(w,nums[i]+nums[j])\n",
    "        return w\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        sum = -1\n",
    "        max_val = [-inf] * 10\n",
    "        for v in nums:\n",
    "            max_d = max(map(int,str(v)))\n",
    "            sum = max(sum,v+max_val[max_d])\n",
    "            max_val[max_d] = max(v,max_val[max_d])\n",
    "        return sum\n",
    "  \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        num_dict = {}\n",
    "        max_sum = -1\n",
    "        for num in nums:\n",
    "            max_char = max(str(num))\n",
    "            num_list = num_dict.setdefault(max_char, [])\n",
    "            bisect.insort(num_list, num)\n",
    "            if len(num_list) > 1:\n",
    "                max_sum = max(max_sum, num_list[-1] + num_list[-2])\n",
    "        return max_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = -1\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                s,t = str(nums[i]),str(nums[j])\n",
    "                ls,lt = list(s),list(t)\n",
    "                ls.sort(reverse=True)\n",
    "                lt.sort(reverse=True)\n",
    "                if ls[0] == lt[0]:\n",
    "                    ans = max(ans,nums[i]+nums[j])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def split_digits(self, num):\n",
    "        dlist = []\n",
    "        nlist = list(str(num))\n",
    "\n",
    "        for i in range(len(nlist)):\n",
    "            dlist.append(int(nlist[i]))\n",
    "        return dlist\n",
    "\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        result = -1\n",
    "\n",
    "        for i in range(len(nums) - 1):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                if max(self.split_digits(nums[i])) == max(self.split_digits(nums[j])):\n",
    "                    result = max(result, nums[i] + nums[j])\n",
    "\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        max_val = [-inf] * 10\n",
    "        for v in nums:\n",
    "            max_d = max(map(int, str(v)))\n",
    "            ans = max(ans, v + max_val[max_d])\n",
    "            max_val[max_d] = max(max_val[max_d], v)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        def count(x):\n",
    "            tmp = 0\n",
    "            while(x):\n",
    "                tmp = max(tmp, x%10)\n",
    "                x//=10\n",
    "            return tmp\n",
    "\n",
    "        _max = -1\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                if count(nums[i]) == count(nums[j]):\n",
    "                    _max = max(_max , nums[i] + nums[j])\n",
    "        return _max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        c=[-inf]*10\n",
    "        max_=-1\n",
    "        for i in nums: \n",
    "            max_num=max(map(int,str(i)))\n",
    "            max_=max(max_,c[max_num]+i)\n",
    "            c[max_num]=max(c[max_num],i)\n",
    "        return max_\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        mx = [0] *10  # 至多 9 个 9 相加\n",
    "        for num in nums:\n",
    "            # s = sum(map(int, str(num)))\n",
    "            # 不转成 str，效率更高\n",
    "            s = 0\n",
    "            t = 0\n",
    "            x = num\n",
    "            while x:  # 枚举 num 的每个数位\n",
    "                s = x % 10\n",
    "                t=max(t,s)\n",
    "                x //= 10\n",
    "            if mx[t]:  # 说明左边也有数位和等于 s 的元素\n",
    "                ans = max(ans, mx[t] + num)  # 更新答案的最大值\n",
    "            mx[t] = max(mx[t], num)  # 维护数位和等于 s 的最大元素\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        c=[-inf]*10\n",
    "        max_=-1\n",
    "        for i in nums: \n",
    "            max_num=max(map(int,str(i)))\n",
    "            max_=max(max_,c[max_num]+i)\n",
    "            c[max_num]=max(c[max_num],i)\n",
    "        return max_\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        c={1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[]}\n",
    "        for i in nums: \n",
    "            c[max(map(int,str(i)))].append(i)\n",
    "        max_=-1\n",
    "        for i in c:\n",
    "            c[i].sort()\n",
    "            if len(c[i])>1:\n",
    "                max_=max(max_,c[i][-1]+c[i][-2])\n",
    "        return max_\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        mx = [0] * 82\n",
    "        for num in nums:\n",
    "            s = 0\n",
    "            x = num\n",
    "            while x:\n",
    "                s = max(s, x % 10)\n",
    "                x //= 10\n",
    "            if mx[s]:\n",
    "                ans = max(ans, mx[s] + num)\n",
    "            mx[s] = max(mx[s], num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        n = len(nums)\n",
    "        \n",
    "        def maxd(x):\n",
    "            c = '0'\n",
    "            while x:\n",
    "                t = str(x % 10)\n",
    "                if c < t:\n",
    "                    c = t\n",
    "                x //= 10\n",
    "            return c\n",
    "                    \n",
    "        \n",
    "        for i in range(n):\n",
    "            mxi = maxd(nums[i])\n",
    "            for j in range(i + 1, n):\n",
    "                mxj = maxd(nums[j])\n",
    "                if mxi == mxj:\n",
    "                    res = max(res, nums[i] + nums[j])\n",
    "        return res\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        max_val = [-inf] * 10\n",
    "        for v in nums:\n",
    "            max_d = max(map(int, str(v)))\n",
    "            if v + max_val[max_d] > ans:\n",
    "                ans = v + max_val[max_d]\n",
    "            if v > max_val[max_d]:\n",
    "                max_val[max_d] = v\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        c={1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[]}\n",
    "        for i in nums: \n",
    "            c[max(map(int,str(i)))].append(i)\n",
    "        max_=-1\n",
    "        for i in c:\n",
    "            c[i].sort()\n",
    "            if len(c[i])>1:\n",
    "                max_=max(max_,c[i][-1]+c[i][-2])\n",
    "        return max_\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        data=defaultdict(int)\n",
    "        ans=-1\n",
    "        for x in nums:\n",
    "            val=max(map(int,str(x)))\n",
    "            if data[val]!=0:\n",
    "                ans=max(ans,data[val]+x)\n",
    "            data[val]=max(data[val],x)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        def max_num(n):#求数位最大值\n",
    "            my_string=str(n)\n",
    "            max_number=0\n",
    "            for i in my_string:\n",
    "                i=int(i)\n",
    "                max_number=max(max_number,i)\n",
    "            return max_number\n",
    "        \n",
    "        ans=-1\n",
    "        for i in range(0,len(nums)-1):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if max_num(nums[i])==max_num(nums[j]):\n",
    "                    ans=max(ans,nums[i]+nums[j])\n",
    "        return ans\n",
    "\n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        sums = [0] * 19\n",
    "        ans = -1\n",
    "        for num in nums:\n",
    "            t = 0\n",
    "            x = num\n",
    "            while x:\n",
    "                t = max(t, x % 10)\n",
    "                x //= 10\n",
    "            if sums[t]:\n",
    "                ans = max(ans, sums[t] + num)\n",
    "            sums[t] = max(sums[t], num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getrank(self,num:int)->int:\n",
    "        res=max(int(x) for x in str(num))\n",
    "        return res\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        maxnum=0\n",
    "        tag=0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if self.getrank(nums[i])==self.getrank(nums[j]):\n",
    "                    maxnum=max(maxnum,nums[i]+nums[j])\n",
    "                    tag=1\n",
    "        if tag==0:return -1\n",
    "        return maxnum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, s: List[int]) -> int:\n",
    "        n = len(s)\n",
    "        ans = -1\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                if max(str(s[i])) == max(str(s[j])):\n",
    "                    ans = max(ans, s[i] + s[j])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        mx = [0] * 82\n",
    "        for num in nums:\n",
    "            s = 0\n",
    "            x = num\n",
    "            while x:\n",
    "                # s += x % 10\n",
    "                s = max(s, x % 10)\n",
    "                x //= 10\n",
    "            if mx[s]:\n",
    "                ans = max(ans, mx[s] + num)\n",
    "            mx[s] = max(mx[s], num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        maxSum = -1\n",
    "        if all(map(lambda x:len(str(x))==1 and len(set(nums)) == len(nums),nums)):\n",
    "            return maxSum\n",
    "        maxNum = lambda x: max(str(x))\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            sameMaxNumAsLeft = list(filter(lambda x: maxNum(nums[i]) == maxNum(x), nums[i+1:]))\n",
    "            if sameMaxNumAsLeft:\n",
    "                tempSum = list(map(lambda x: nums[i] + x, sameMaxNumAsLeft))\n",
    "                maxSum = max(tempSum) if max(tempSum) > maxSum else maxSum\n",
    "            else:\n",
    "                continue\n",
    "        return maxSum\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        max_val = [-inf] * 10\n",
    "        for v in nums:\n",
    "            max_d = max(map(int, str(v)))\n",
    "            ans = max(ans, v + max_val[max_d])\n",
    "            max_val[max_d] = max(max_val[max_d], v)\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        mx = [0]*10\n",
    "        ans = -1\n",
    "        for num in nums:\n",
    "            x = num\n",
    "            s = 0\n",
    "            while x:\n",
    "                s= max(s,x%10)\n",
    "                x//=10\n",
    "            if mx[s]:\n",
    "                ans = max(ans,mx[s]+num)\n",
    "            mx[s] = max(mx[s],num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        max_val = [float(\"-inf\")]*10\n",
    "        for v in nums:\n",
    "            max_d = max(map(int,str(v)))\n",
    "            ans = max(ans,v+max_val[max_d])\n",
    "            max_val[max_d] = max(max_val[max_d],v)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        res=-1\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i!=j and max([int(_) for _ in str(nums[i])])==max([int(_) for _ in str(nums[j])]):\n",
    "                    res=max(nums[i]+nums[j],res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "      def max_digit(num):\n",
    "        return max(map(int, str(num)))\n",
    "\n",
    "      digit_buckets = {}\n",
    "      for num in nums:\n",
    "          digit = max_digit(num)\n",
    "          if digit not in digit_buckets:\n",
    "              digit_buckets[digit] = []\n",
    "          digit_buckets[digit].append(num)\n",
    "      \n",
    "      max_sum = -1\n",
    "      for digits in digit_buckets.values():\n",
    "          if len(digits) > 1:\n",
    "              digits.sort(reverse=True)\n",
    "              max_sum = max(max_sum, digits[0] + digits[1])\n",
    "      return max_sum\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        c={1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[]}\n",
    "        for i in nums: \n",
    "            c[max(map(int,str(i)))].append(i)\n",
    "        max_=-1\n",
    "        for i in c:\n",
    "            c[i].sort()\n",
    "            if len(c[i])>1:\n",
    "                max_=max(max_,c[i][-1]+c[i][-2])\n",
    "        return max_\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "import heapq\n",
    "from collections import *\n",
    "from functools import *\n",
    "from heapq import heapify, heappop\n",
    "from math import sqrt, ceil\n",
    "from typing import List\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        res=-1\n",
    "        def add(nums1: int)-> int:\n",
    "            ans=0\n",
    "            while(nums1>0):\n",
    "                ans=max(ans,nums1%10)\n",
    "                nums1//=10\n",
    "            return ans\n",
    "        dic={}\n",
    "        for i in range(len(nums)):\n",
    "            if(add(nums[i]) in dic):\n",
    "                res=max(res,dic[add(nums[i])]+nums[i])\n",
    "                dic[add(nums[i])]=max(dic[add(nums[i])],nums[i])\n",
    "            else:\n",
    "                dic[add(nums[i])]=nums[i]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        mm = defaultdict(list)\n",
    "\n",
    "        def get(x):\n",
    "            v = 0\n",
    "            while x:\n",
    "                v = max(v, x % 10)\n",
    "                x //= 10\n",
    "            return v\n",
    "        \n",
    "        for nn in nums:\n",
    "            mm[get(nn)].append(nn)\n",
    "        ans = -1\n",
    "        for k in mm.keys():\n",
    "            if len(mm[k]) > 1:\n",
    "                mm[k].sort()\n",
    "                print(mm[k])\n",
    "                ans = max(ans, mm[k][-1] + mm[k][-2])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        max_val = [-inf] * 10\n",
    "        maxNum = lambda x: max(str(x))\n",
    "        for v in nums:\n",
    "            max_d = int(maxNum(v))\n",
    "            ans = max(ans, v + max_val[max_d])\n",
    "            max_val[max_d] = max(max_val[max_d], v)\n",
    "        return ans\n",
    " \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from copy import deepcopy\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        maxNum = lambda x: max(str(x))\n",
    "        maxSum = -1\n",
    "        for i in range(len(nums)):\n",
    "            tempNums = deepcopy(nums)\n",
    "            tempNums.remove(nums[i])\n",
    "            sameMaxNumAsLeft = list(filter(lambda x: maxNum(nums[i]) == maxNum(x), tempNums))\n",
    "            if sameMaxNumAsLeft:\n",
    "                tempSum = list(map(lambda x: nums[i] + x, sameMaxNumAsLeft))\n",
    "                maxSum = max(tempSum) if max(tempSum) > maxSum else maxSum\n",
    "            else:\n",
    "                continue\n",
    "        return maxSum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        res=-1\n",
    "        for i in range(len(nums)):\n",
    "            s1=str(nums[i])\n",
    "            c1=ord(s1[0])\n",
    "            for k in range(len(s1)):\n",
    "                if c1<ord(s1[k:k+1]):\n",
    "                    c1=ord(s1[k:k+1])\n",
    "            for j in range(i+1,len(nums)):\n",
    "                s2=str(nums[j])\n",
    "                c2=ord(s2[0])\n",
    "                for k in range(len(s2)):\n",
    "                    if c2<ord(s2[k:k+1]):\n",
    "                        c2=ord(s2[k:k+1])\n",
    "                if c1==c2:\n",
    "                    res=max(res,nums[i]+nums[j])\n",
    "                \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        def get_max(n: int):\n",
    "            ma=0\n",
    "            while n!=0:\n",
    "                if n%10>ma:\n",
    "                    ma=n%10\n",
    "                n=int(n/10)\n",
    "            return ma\n",
    "        ans=-1\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if get_max(nums[i])==get_max(nums[j]):\n",
    "                    if nums[i]+nums[j]>ans:\n",
    "                        ans=nums[i]+nums[j]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            a = max(set(str(nums[i])))\n",
    "            for j in range(i + 1, n):\n",
    "                b = max(set(str(nums[j])))\n",
    "                if a == b:\n",
    "                    ans = max(ans, nums[i] + nums[j])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        \n",
    "        new_nums = nums.copy()\n",
    "        \n",
    "        new_nums = [\n",
    "            max(list(map(int, list(str(item)))))\n",
    "            for item in new_nums\n",
    "        ]\n",
    "        \n",
    "        res = -1\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1, len(nums)):\n",
    "                if new_nums[i] == new_nums[j]:\n",
    "                    res = max(res, nums[i] + nums[j])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        dct = defaultdict(list)\n",
    "        for x in nums: dct[max(str(x))].append(x)\n",
    "        return max([sum(sorted(l)[-2:]) for l in dct.values() if len(l) > 1], default = -1)\n",
    " \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        val = [0] * 10\n",
    "        ans = -1\n",
    "        for num in nums:\n",
    "            t, cur = num, 0\n",
    "            while t:\n",
    "                cur = max(cur, t % 10)\n",
    "                t //= 10\n",
    "            if val[cur]:\n",
    "                ans = max(ans, num + val[cur])\n",
    "            val[cur] = max(num, val[cur])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        val = [0] * 10\n",
    "        ans = -1\n",
    "        for num in nums:\n",
    "            cur = max(map(int, str(num)))\n",
    "            if val[cur]:\n",
    "                ans = max(ans, num + val[cur])\n",
    "            val[cur] = max(num, val[cur])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        dct = defaultdict(list)\n",
    "        for x in nums: dct[max(str(x))].append(x)\n",
    "        return max([sum(sorted(v)[-2:]) for v in dct.values() if len(v) > 1], default = -1)\n",
    " \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        max_val = [-inf] * 10\n",
    "        for v in nums:\n",
    "            max_d = max(map(int, str(v)))\n",
    "            ans = max(ans, v + max_val[max_d])\n",
    "            max_val[max_d] = max(max_val[max_d], v)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        max_val = [-inf] * 10\n",
    "        for v in nums:\n",
    "            max_d = max(map(int, str(v)))\n",
    "            if v + max_val[max_d] > ans:\n",
    "                ans = v + max_val[max_d]\n",
    "            if v > max_val[max_d]:\n",
    "                max_val[max_d] = v\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        dp = [0] * 10\n",
    "        ans = -1\n",
    "        for x in nums:\n",
    "            m = 0\n",
    "            num = x\n",
    "            while x > 0:\n",
    "                m = max(x % 10, m)\n",
    "                x //= 10\n",
    "            if dp[m]:\n",
    "                ans = max(dp[m]+num, ans)\n",
    "            dp[m] = max(dp[m], num)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        try:\n",
    "            return max(x + y for x, y in combinations(nums, 2) if max(int(c) for c in str(x)) == max(int(c) for c in str(y)))\n",
    "        except:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = -1\n",
    "\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                s1,s2 = max(str(nums[i])),max(str(nums[j]))\n",
    "                if s1[0]==s2[0]:\n",
    "                    res = max(res,nums[i]+nums[j])\n",
    "            \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        maxSum = -1\n",
    "        if all(map(lambda x:len(str(x))==1 and len(set(nums)) == len(nums),nums)):\n",
    "            return maxSum\n",
    "        maxNum = lambda x: max(str(x))\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            sameMaxNumAsLeft = list(filter(lambda x: maxNum(nums[i]) == maxNum(x), nums[i+1:]))\n",
    "            if sameMaxNumAsLeft:\n",
    "                tempSum = list(map(lambda x: nums[i] + x, sameMaxNumAsLeft))\n",
    "                maxSum = max(tempSum) if max(tempSum) > maxSum else maxSum\n",
    "            else:\n",
    "                continue\n",
    "        return maxSum\n",
    " \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        \n",
    "        def getmax(x):\n",
    "            ans = 0\n",
    "            while x:\n",
    "                c = x % 10\n",
    "                ans = max(ans, c)\n",
    "                x //= 10\n",
    "            return ans\n",
    "        \n",
    "        d = dict()\n",
    "        \n",
    "        ans = -1\n",
    "        for x in nums:\n",
    "            mx = getmax(x)\n",
    "            if mx in d:\n",
    "                ans = max(ans, x + d[mx])\n",
    "                d[mx] = max(d[mx], x)\n",
    "            else:\n",
    "                d[mx] = x\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from copy import deepcopy\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        maxSum = -1\n",
    "        if all(map(lambda x:len(str(x))==1 and len(set(nums)) == len(nums),nums)):\n",
    "            return maxSum\n",
    "        maxNum = lambda x: max(str(x))\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            sameMaxNumAsLeft = list(filter(lambda x: maxNum(nums[i]) == maxNum(x), nums[i+1:]))\n",
    "            if sameMaxNumAsLeft:\n",
    "                tempSum = list(map(lambda x: nums[i] + x, sameMaxNumAsLeft))\n",
    "                maxSum = max(tempSum) if max(tempSum) > maxSum else maxSum\n",
    "            else:\n",
    "                continue\n",
    "        return maxSum\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        max_val = [float(\"-INF\")] * 10\n",
    "        for v in nums:\n",
    "            max_d = max(map(int, str(v)))\n",
    "            ans = max(ans, v + max_val[max_d])\n",
    "            max_val[max_d] = max(max_val[max_d], v)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maxSum(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        h = [0] * 10\n",
    "        for num in nums:\n",
    "            s = 0\n",
    "            x = num\n",
    "            maxx = 0\n",
    "            while x:\n",
    "                s += x%10\n",
    "                maxx = max(maxx, x%10)\n",
    "                x //= 10\n",
    "            if h[maxx]:\n",
    "                res = max(res, h[maxx] + num)\n",
    "            h[maxx] = max(h[maxx], num)\n",
    "        return res"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
